/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formListaCliente.java
 *
 * Created on 06/09/2011, 15:57:59
 */
package javalee.forms;

import java.util.ArrayList;
import java.util.Iterator;
import javalee.util.Uteis;
import javalee.dao.clienteDAO;
import javalee.maps.Cliente;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Danilo
 */
public class formListaCliente extends javax.swing.JFrame {
    Cliente Cli;
    private clienteDAO cliDAO;
    /** Creates new form formListaCliente */
    public formListaCliente(Cliente Cl) {
        cliDAO = new clienteDAO();
        Cli = Cl;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancelar = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbConsulta = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        txtPesquisa = new javax.swing.JTextField();

        setTitle("Formulario de Lista de Clientes");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnOk.setText("Ok");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        tbConsulta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Id", "Nome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbConsulta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tbConsultaMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tbConsulta);

        jLabel3.setText("Pesquisar cliente :");

        txtPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPesquisaActionPerformed(evt);
            }
        });
        txtPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPesquisaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addComponent(txtPesquisa))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 183, Short.MAX_VALUE)
                .addComponent(btnOk)
                .addGap(76, 76, 76))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnOk))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    dispose();
    // TODO add your handling code here:
}//GEN-LAST:event_btnCancelarActionPerformed

private void tbConsultaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbConsultaMouseReleased
    int row = tbConsulta.getSelectedRow();
    
    DefaultTableModel tableModel = (DefaultTableModel)tbConsulta.getModel();  
   
}//GEN-LAST:event_tbConsultaMouseReleased

private void txtPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPesquisaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtPesquisaActionPerformed

private void txtPesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPesquisaKeyReleased
    if(evt.getKeyCode()==10){
        preencheDados();
    }
}//GEN-LAST:event_txtPesquisaKeyReleased

private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    preencheDados();
    // TODO add your handling code here:
}//GEN-LAST:event_formWindowActivated

private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
int row = tbConsulta.getSelectedRow();
    
    DefaultTableModel tableModel = (DefaultTableModel)tbConsulta.getModel();
    Cli.setId(Integer.parseInt(tableModel.getValueAt(row, 0).toString()));
    Cli.setNome(tableModel.getValueAt(row, 1).toString());
    dispose();
}//GEN-LAST:event_btnOkActionPerformed

private void preencheDados()
    {
        if(!Uteis.validaCampoTexto(txtPesquisa)){
            JOptionPane.showMessageDialog(null,"Campo de pesquisa está incorreto.");
        }
        else{
        //Trago a lista de clientes do banco
        ArrayList<Cliente> lista = cliDAO.consultar(txtPesquisa.getText().toString());
        tbConsulta.removeAll();
     
        //Pego o TableModel do componente JTable
        DefaultTableModel tableModel = (DefaultTableModel)tbConsulta.getModel();  
        //Limpo os dados do componente
        tableModel.setNumRows(0);
     
        //Passo pela lista de Clientes, inserindo no JTable
        for(Iterator<Cliente> it = lista.iterator(); it.hasNext();){
            Cliente c = (Cliente)it.next();
            tableModel.addRow(new Object[]{
                c.getId(),//Cada campo que coloco vira uma coluna
                c.getNome()
            });     
        }
    }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnOk;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbConsulta;
    private javax.swing.JTextField txtPesquisa;
    // End of variables declaration//GEN-END:variables
}
